home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15420 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is this a memory leak?
  5. Date: 5 Apr 1996 21:34:28 +1000
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4k30g4$sdo@werple.net.au>
  8. References: <4jv214$gv7@insosf1.netins.net> <4k114a$m5d@linet06.li.net>
  9. NNTP-Posting-Host: werplez.mira.net.au
  10.  
  11. jeremy@newshost.li.net (Jeremy Markman) writes:
  12.  
  13. >Harold Howe (hhowe@trgnet.com) wrote:
  14. >: Could someone please tell me if this code leaks memory
  15.  
  16.  
  17. >:   public:
  18. >:     TopClass::TopClass() { bury = new BuriedClass();}
  19. >:     shutDown             { bury = 0;}
  20. >:     ~TopClass            { delete bury}
  21.  
  22. >: Is the memory that was alloced for the buried class lost? If not please 
  23. >: describe how it was freed.  Does zeroing the pointer free the memory? The 
  24. >: delete is a waste of time on a zeroed pointer, isn't it.
  25.  
  26. >Yes, your are correct.  By assigning bury to 0, you are effectively 
  27. >reassigning the memory pointer to NULL.  Therefore, deleting bury has no 
  28. >effect.  The shutdown function is not necessary, in fact, it is bad 
  29. >practice to do that.  Just the delete bury is necessary, and if you do 
  30. >want to assign 0 to a pointer, use NULL.
  31.  
  32. The shutdown function does seem unusual practice, but surely whether it is
  33. bad practice in the context of its use depends on its purpose, which has
  34. not been established. 
  35.  
  36. Also, many prominent people involved in C++, among them Bjarne Stroustrup,
  37. the language's designer, recommend using 0, not NULL, as a null pointer.
  38. One of the arguments against NULL is that it is not as portable as 0; some
  39. people might spell it 'Null', or 'null'. 
  40.  
  41. >More explanation:
  42. >new allocattes memory and delete deallocates it.  If you change the 
  43. >pointer to that it does not point to the allocated memory, it can't 
  44. >deallocate it, now can it?
  45.  
  46. >Hope this helps...
  47.  
  48. David White
  49. davidw@werple.mira.net.au
  50.